#!/bin/sh
#
#    updateWS.sh  WorkingEnvironment  Project  [SSWE|SOWE|PWE]

rm -f update.log
rm -f updateSniff.log
touch update.log
touch updateSniff.log

if [ "$1" = "" -o "$2" = "" -o "$3" = "" ]
then
	echo "Usage: updateWS.sh  WorkingEnvironment  Project  [SSWE|SOWE|PWE]";
	exit
fi

USER=${USER:-$LOGNAME}

# change subdirectory if project (second command line parameter) isn't 
# in the root directory of workspace
basedir=`pwd`
cd `dirname $2`

echo "==================================================================="	>> $basedir/update.log 2>&1
echo "Start update of project $2 in the Working Environment $1"				>> $basedir/update.log 2>&1
date																		>> $basedir/update.log 2>&1
echo "==================================================================="	>> $basedir/update.log 2>&1

PATH=/usr/local/bin:$PATH

MAKE=gmake
export MAKE


# set SNIFF_DIR environment variable
# if already set use it
# if not found retrive it from the script calling line
if [ -z "${SNIFF_DIR:-}" ]
then
	SNIFF_DIR=`dirname $0 | sed -e 's#/ws_support##g'`
	export SNIFF_DIR
fi
echo "EnvVar SNIFF_DIR: $SNIFF_DIR"											 >> $basedir/update.log 2>&1
 
# add bin directory of SNiFF+
PATH=$SNIFF_DIR/bin:$PATH
export PATH
 
# set PLATFORM environment variable for make support
if [ -z "${PLATFORM:-}" ]
then
	PLATFORM=`sniff_arch`
	export PLATFORM
fi
echo "EnvVar PLATFORM: $PLATFORM"											 >> $basedir/update.log 2>&1
 
# set session identifier
session=`pwd`
session="cron$3_`basename $session`"

if [ "$3" = "SOWE" ]
then
# delete symbols of SNiFF+ in the workspace (.sniffdir) recursively.
# SNiFF+ will then use shared symbols
	echo $MAKE -i sniffclean										 		>> $basedir/update.log 2>&1
	$MAKE -i sniffclean												 		>> $basedir/update.log 2>&1
fi


# start sniff and sniffaccess in Batch mode (SNIFF_BATCH=1)
SNIFF_BATCH=1
export SNIFF_BATCH

# remove old appcomm file (if there)
appFile=sniffappcomm_"$session"_$USER
rm -f "/tmp/.sniffdir2/$appFile" 											>> $basedir/update.log 2>&1

sniff -s $session -l $basedir/updateSniff.log & 							>> $basedir/update.log 2>&1
sleep 120

# clean old cache directory
if [ -r $basedir/.ProjectCache ]
then
	rm -f $basedir/.ProjectCache/* 											>> $basedir/update.log 2>&1
fi

if [ -r /tmp/sniffacess_"$session"_$USER ]
then
	rm -f /tmp/sniffacess_"$session"_$USER 									>> $basedir/update.log 2>&1
fi

# open project in SNiFF+ and update the workspace and the makefiles
# update configuration information and generate xref information new
echo "set_timeout 2000          # set timeout to 2000 sec"			>  /tmp/sniffacess_"$session"_$USER
echo set_workingenv \"$1\"											>> /tmp/sniffacess_"$session"_$USER
if [ "$3" = "SSWE" -o "$3" = "PWE" ]
then
	echo open_project \"$2\" NO_CACHE								>> /tmp/sniffacess_"$session"_$USER
else
	echo open_project \"$2\" WITHOUT_SYMBOLS NO_CACHE				>> /tmp/sniffacess_"$session"_$USER
fi
if [ "$3" != "SOWE" ]
then
	echo "update_private_we */      # update workingenv"			>> /tmp/sniffacess_"$session"_$USER
	echo "reload_project */"										>> /tmp/sniffacess_"$session"_$USER
fi
echo "update_makefiles */"											>> /tmp/sniffacess_"$session"_$USER
if [ "$3" != "SOWE" ]
then
	echo "update_cms_info */   # update configuration information"	>> /tmp/sniffacess_"$session"_$USER
	echo "generate_xref_info */"									>> /tmp/sniffacess_"$session"_$USER
fi
echo check_obsolete_files \"`basename $2`\"							>> /tmp/sniffacess_"$session"_$USER
echo quit															>> /tmp/sniffacess_"$session"_$USER

 
sniffaccess -s $session < /tmp/sniffacess_"$session"_$USER 					>> $basedir/update.log 2>&1
 
rm -f /tmp/sniffacess_"$session"_$USER 										>> $basedir/update.log 2>&1

if [ "$3" != "SSWE" ]
then
	if [ "$3" = "PWE" ]
	then
		# clean all targets recursively
		# make symbolic links to shared object files.
		echo $MAKE -i clean													>> $basedir/update.log 2>&1
		$MAKE -i clean														>> $basedir/update.log 2>&1
		echo $MAKE -i symbolic_links								 		>> $basedir/update.log 2>&1
		$MAKE -i symbolic_links										 		>> $basedir/update.log 2>&1
	fi
	if [ "$3" = "SOWE" ]
	then
		# make symbolic link to the shared dependencies file recursively
		# make symbolic links to shared object files.
		# Symbolic links are only needed if there are multiple shared object 
		# workspaces in the working environment
		echo $MAKE -i symbolic_link_to_dependencies_file					>> $basedir/update.log 2>&1
		$MAKE -i symbolic_link_to_dependencies_file					 		>> $basedir/update.log 2>&1
		#$MAKE -i symbolic_links									 		>> $basedir/update.log 2>&1
	fi
	# start make recursively
	echo $MAKE -i CHECK_UPDATE=0											>> $basedir/update.log 2>&1
	$MAKE -i CHECK_UPDATE=0											 		>> $basedir/update.log 2>&1
	ret=$?
else
	ret=0
fi

echo "==================================================================="	>> $basedir/update.log 2>&1
echo "End of update: "														>> $basedir/update.log 2>&1
date																		>> $basedir/update.log 2>&1
echo "==================================================================="	>> $basedir/update.log 2>&1

if [ -x /usr/lib/sendmail ]
then
	(echo "Subject: Update in the WE '$1'"; echo To: $USER; echo ''; cat $basedir/updateSniff.log $basedir/update.log) | /usr/lib/sendmail -F$3 $USER
fi

exit $ret
